/**
 * 
 */
package wong.board.client.ui;

import wong.board.client.Board;
import wong.board.client.resources.BoardBundle.BoardCss;
import wong.board.client.ui.ValidationPresenter.View;
import wong.board.client.widgets.Container;
import wong.board.client.widgets.Table;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;

/**
 * @author wong
 * 
 */
public class ValidationView extends Container implements View {
	private static BoardCss boardCss = Board.getBundle().boardCss();
	private Table table;
	private boolean isSetup = false;
	private FlowPanel statusCell;
	private FlowPanel infoCell;
	private Table infoTable;
	private Label userAgentLabel;
	private Label userAgentText;
	private Label appLabel;
	private Label appText;
	private Label systemLabel;
	private Label systemText;
	private Label statusLabel;
	private Label statusText;
	private State state = State.Undefined;

	/**
	 * 
	 */
	public ValidationView() {
		this.addStyleName(boardCss.validation());
		table = new Table();
		table.addStyleName(boardCss.validationTable());

		getContainer().add(table);

		setupView();
	}

	private void setupView() {
		if (isSetup)
			return;

		statusLabel = new Label("Status");
		statusLabel.setStyleName(boardCss.validationLabel());
		statusText = new Label();
		statusText.setStyleName(boardCss.validationText());

		userAgentLabel = new Label("User Agent");
		userAgentText = new Label();
		appLabel = new Label("App");
		appText = new Label();
		systemLabel = new Label("System");
		systemText = new Label();

		infoTable = new Table();
		infoTable.addStyleName(boardCss.validationInfoTable());

		createRow(userAgentLabel, userAgentText);
		createRow(appLabel, appText);
		createRow(systemLabel, systemText);

		statusCell = new FlowPanel();
		statusCell.addStyleName(boardCss.validationStatus());
		statusCell.add(statusLabel);
		statusCell.add(statusText);

		infoCell = new FlowPanel();
		infoCell.setStyleName(boardCss.validationInfo());
		infoCell.add(infoTable);

		table.addRowData(statusCell, infoCell);

		isSetup = true;
	}

	private void createRow(Label label, Label text) {
		label.addStyleName(boardCss.validationLabel());
		text.addStyleName(boardCss.validationText());
		infoTable.addRowData(label, text);
	}

	@Override
	public HasText getUserAgentText() {
		return userAgentText;
	}

	@Override
	public HasText getAppText() {
		return appText;
	}

	@Override
	public HasText getSystemText() {
		return systemText;
	}

	@Override
	public HasText getStatusText() {
		return statusText;
	}

	@Override
	public void setState(State newState) {
		assert newState != null : "new state must not be null";
		if (newState != this.state) {
			updateStyle(true);
			this.state = newState;
			updateStyle(false);
		}
	}

	protected void updateStyle(boolean remove) {
		String style;
		switch (this.state) {
		case Valid:
			style = boardCss.stateValid();
			break;
		case Invalid:
			style = boardCss.stateInvalid();
			break;
		case Other:
			style = boardCss.stateOther();
			break;

		default:
			style = null;
			break;
		}
		if (style != null) {
			if (remove) {
				this.removeStyleName(style);
			} else {
				this.addStyleName(style);
			}
		}
	}

}
